<?php


namespace app\admin\controller;


use app\common\controller\AdminUserBase;
use app\common\model\Admin;
use app\common\model\ShopCate;
use think\Db;
use think\facade\Request;
use think\Validate;

class Shop extends AdminUserBase
{
    /**
     * 分类
     */
    public function cate(){
        if (Request::isAjax()){
            $param = input();
            $where = [];
            $data = ShopCate::where($where)->select();
            $this->result($data);
        }
        return view('shop/cate/index');
    }
    public function cate_form(){
        $id = input('id');
        $data = ShopCate::get($id);
        $this->assign('data',$data);
        return view('shop/cate/form');
    }
    public function cate_save(){
        $param = input();
        $model = new ShopCate();

        if (empty($param['id'])){
            $param['admin_id'] = session('admin.id');
            $res = $model->allowField(true)->isUpdate(false)->save($param);
            saveAdminLog("添加店铺分类");
        }else{
            $res = $model->allowField(true)->isUpdate(true)->save($param,['id'=>$param['id']]);
            saveAdminLog("编辑店铺分类");
        }

        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }
    public function cate_del(){
        $id = input('id');
        $res = ShopCate::destroy($id);
        if ($res !== false){
            saveAdminLog("删除店铺分类");
            $this->success();
        }else{
            $this->error();
        }
    }
    /**
     * 分类
     */

    /**
     * 店铺
     */
    public function shop(){
        if (Request::isAjax()){
            $param = input();
            $where = [];
            if (!empty($param['title'])){
                $where[] = ['title','like','%'.$param['title'].'%'];
            }
            if (!empty($param['top'])){
                $where[] = ['top','=',$param['top']];
            }
            if (!empty($param['show'])){
                $where[] = ['show','=',$param['show']];
            }
            $data = \app\common\model\Shop::where($where)->order('sort desc')->paginate($param['limit']);
            $data = $data->toArray();
            $this->result($data['data'],$data['total']);
        }
        return view('index');
    }
    public function form(){
        $id = input('id');
        $data = \app\common\model\Shop::with(['admin'])->where('id','=',$id)->find();
        $data['password'] = encry_code($data['password'],'DECODE');
        $this->assign('data',$data);


        $shopCate = ShopCate::select();
        $this->assign('shopCate',$shopCate);
        return view();
    }
    public function shop_save(){
        $param = input();
        Db::startTrans();
        try {
//            校验shop表内的手机号
            $validate = Validate::make([
                'phone|手机号'  => 'require|unique:shop',
            ]);
            if (!$validate->check($param)) {
                abort(1,$validate->getError());
            }

            //            校验admin表内的用户名
            $validate = Validate::make([
                'username|管理员账号'  => 'require|unique:admin',
            ]);
            if (!$validate->check(['username'=>$param['phone'],'id'=>$param['admin_id']])) {
                abort(1,$validate->getError());
            }

            $model = new \app\common\model\Shop();

            $param['password'] = encry_code($param['password']);
            if (empty($param['id'])){
                $adminModel = new Admin();
                $adminModel->username = $param['username'];
                $adminModel->password = $param['password'];
                $adminModel->nick_name = $param['name'];
                $adminModel->save();
                $param['admin_id'] = $adminModel->id;
                $model->data($param)->allowField(true)->isUpdate(false)->save();
                saveAdminLog("添加店铺");
            }else{
                $adminModel = Admin::get($param['admin_id']);
                $adminModel->username = $param['username'];
                $adminModel->password = $param['password'];
                $adminModel->nick_name = $param['name'];
                $adminModel->save();
                $model->allowField(true)->isUpdate(true)->together(['admin'=>['username','password']])->save($param);
                saveAdminLog("编辑店铺");

//                将该店铺下所有的商品的经纬度同步一遍
                \app\common\model\Goods::where('shop_id','=',$param['id'])->update([
                    'lng'=>$param['lng'],
                    'lat'=>$param['lat'],
                    'province'=>$param['province'],
                    'city'=>$param['city'],
                    'area'=>$param['area'],
                ]);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();
    }
    public function shop_edit_save(){
        $param = input();
        $shop = new \app\common\model\Shop();
        $res = $shop->allowField(true)->isUpdate(true)->save($param,['id'=>$param['id']]);
        if ($res !== false){
            saveAdminLog("编辑店铺");
            $this->success();
        }else{
            $this->error();
        }
    }
    public function shop_del(){
        $id = input('id');
        if (empty($id)){
            $id = input('ids');
        }
        $res = \app\common\model\Shop::destroy($id);
        if ($res !== false){
            saveAdminLog("删除店铺");
            $this->success();
        }else{
            $this->error();
        }
    }
    /**
     * 店铺
     */
}
